﻿#include "form485.h"
#include "ui_form485.h"
#include<QSerialPortInfo>



#ifdef Q_CC_MSVC
#pragma execution_character_set("utf-8")
#endif



Form485::Form485(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::Form485)
{
	ui->setupUi(this);


	lds = new Rs485Com;
	this->refreshPortList();
	this->connectSlots();

	QCPGraph* graph = ui->widget_chart->addGraph();

}

Form485::~Form485()
{
	delete ui;
}


void Form485::refreshPortList(void)
{
	ui->comboBox->clear();
	QStringList portList;
	foreach(const QSerialPortInfo & info, QSerialPortInfo::availablePorts())
	{
		portList.append(info.portName());
	}
	ui->comboBox->addItems(portList);
}

void Form485::connect2SensorSlots(void)
{
	emit connect2Sensor(ui->comboBox->currentText());
}


void Form485::connectSlots(void)
{
	connect(ui->refreshButton, &QPushButton::clicked, this, &Form485::refreshPortList);


	connect(this, &Form485::connect2Sensor, lds, &Rs485Com::initPort);


	connect(lds, &Rs485Com::failed, this, [=] {
		qDebug() << "连接错误";
		});
	connect(lds, &Rs485Com::succeed, this, [=] {
		qDebug() << "连接成功";
		});



	connect(ui->connectButton, &QPushButton::clicked, this, &Form485::connect2SensorSlots);

}

//设置速度
void Form485::on_pushButton_set_speed_clicked()
{
	QString str_speed = ui->lineEdit_speed->text();
	int speed = str_speed.toInt();
	ui->widget_chart->yAxis->setRange(-0, speed * 1.5);

    //设置目标转速 RPM
	unsigned char hex_0[8]{ 0X04, 0X06 , 0X20 , 0X3A , 0X00 , 0X10, 0X63 , 0XC4 };
	unsigned char* s = (unsigned char*)&speed;
	hex_0[5] = s[0];
	hex_0[4] = s[1];

	lds->send_order(hex_0, 8);

	//设置目标转速-100RPM
//	unsigned char hex_0[8]{ 0X04, 0X06 , 0X20 , 0X3A , 0XFF , 0X9c, 0X63 , 0XC4 };
//	lds->send_order(hex_0, 8);


}

//设置速度模式
void Form485::on_pushButton_set_mode_speed_clicked()
{
	unsigned char hex_0[8]{ 0X04, 0X06 , 0X20 , 0X32 , 0X00 , 0X03, 0X63 , 0XC4 };
	lds->send_order(hex_0, 8);

}

// 01 06 20 31 00 08 D2 03
//启动
void Form485::on_pushButton_power_on_clicked()
{  	//设置S型加速时间500ms
//	unsigned char hex_0[8]{ 0X04, 0X06 , 0X20 , 0X37 , 0X01 , 0XF4, 0X63 , 0XC4 };
//	lds->send_order(hex_0, 8);

	unsigned char hex_1[8]{ 0X04, 0X06 , 0X20 , 0X31 , 0X00 , 0X08, 0X63 , 0XC4 };
	lds->send_order(hex_1, 8);
}


void Form485::on_pushButton_sync_clicked()
{
	//th = new SubThread;
	//th->com485 = lds;
	QString str_speed = ui->lineEdit_speed->text();
	int speed = str_speed.toInt();

	ui->widget_chart->yAxis->setRange(-0, speed * 1.5);

	connect(lds, &Rs485Com::result, this, [=](int addr, double d) {
		switch (addr)
		{
		case 0:
			ui->d1Edit->setText(QString::number(d));
			break;
		case 1:
			ui->d2Edit->setText(QString::number(d));
			break;
		case 2:
			ui->d3Edit->setText(QString::number(d));
			break;
		case 3:
			ui->d4Edit->setText(QString::number(d));
			break;

		}
		int cnt = ui->widget_chart->graph(0)->data()->size();
		ui->widget_chart->graph(0)->addData(cnt, d);
		if (cnt < 100)
			ui->widget_chart->xAxis->setRange(0, cnt);
		else
			ui->widget_chart->xAxis->setRange(cnt - 100, cnt);

		ui->widget_chart->replot();
		});

	lds->start();

}

